package com.koron.common.core.business.print.printstandard;

import com.koron.common.core.business.print.bean.PrintBusinessBean;
import com.koron.common.core.business.print.bean.PrintTemplateBean;
import com.koron.common.core.business.print.bean.dto.PrintTemplateDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


/**
 * 打印标准维护
 * @author 付浩强
 *
 */
public interface PrintStandardMapper {


	/**
	 * 查询打印业务下面所有的打印模版
	 * @param businessCode 业务编号
	 * @return
	 */
	@Select("SELECT id, app_code, name, default_flag,remark,status FROM c_print_template WHERE business_id = "
			+ " (select id from c_print_business where code =#{businessCode} and delete_flag=0) "
			+ "AND delete_flag = 0 "
			+ "ORDER BY create_time")
	List<PrintTemplateBean> getTemplateListByBusinessCode(@Param("businessCode") String businessCode);

	/**
	 * 查询报表的字段列表
	 * @param businessId 业务id
	 * @return
	 */
	@Select("SELECT field FROM c_print_business  WHERE id=#{businessId}")
	String getField(@Param("businessId") String businessId);


	@Select("SELECT field from c_print_business where id=(select business_id from c_print_template where id=#{id} ) " )
	String  getFieldByTemplateId(@Param("id") String id);

	/**
	 * 查询打印业务下面所有的打印模版
	 * @param businessId
	 * @return
	 */
	@Select("SELECT id, name, default_flag FROM c_print_template WHERE business_id = #{businessId} AND delete_flag = 0")
	List<PrintTemplateBean> getTemplateListByBusiness(@Param("businessId") String businessId);


//	/**
//	 * 查询打印业务下面所有的打印模版
//	 * @param businessId
//	 * @return
//	 */
//	@Select("SELECT * FROM c_print_template WHERE business_id = #{businessId} AND delete_flag = 0 ORDER BY create_time")
//	List<PrintTemplateBean> readTemplateList(@Param("businessId") String businessId);

	//查询模板列表
	List<PrintTemplateDto> queryAllByBusinessGroup(@Param("businessGroup")String businessGroup, @Param("name") String name);


	/**
	 * 查询打印模版
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM c_print_template WHERE id = #{id} ")
	PrintTemplateBean getTemplateListById(@Param("id") String id);

	/**
	 * 查询打印模版
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM c_default_print_template WHERE id = #{id} ")
	PrintTemplateBean getDefaultTemplateListById(@Param("id") String id);

	/**
	 * 创建初始模板
	 * @param printTemplate
	 * @return
	 */
	@Insert("INSERT INTO c_print_template (id,name,content,business_id,default_flag,remark,business_code,business_group,create_by,create_by_name,create_time,delete_flag,status) "
			+ " values(#{id},#{name},#{content},#{businessId},#{defaultFlag},#{remark},#{businessCode},#{businessGroup},#{createBy},#{createByName},now(),#{deleteFlag},#{status})")
	int insertInitTemplate(PrintTemplateBean printTemplate);


	/**
	 * 更新模板内容
	 * @param id
	 * @param content
	 * @return
	 */
	@Update("UPDATE c_print_template SET content = #{content}, update_time = NOW() WHERE id = #{id}")
	int updateTemplateContent(@Param("id") String id, @Param("content") byte[] content);

	/**
	 * 获取打印业务列表
	 * @return
	 */
	@Select("SELECT * FROM c_print_business WHERE delete_flag = 0 ORDER BY create_time")
	List<PrintBusinessBean> readPrintBusList();

	/**
	 * 添加打印业务
	 * @param bean
	 * @return
	 */
	@Insert("INSERT INTO c_print_business "
			+ "(id, name, code, field, remark, create_by, create_time, status, delete_flag) "
			+ "VALUES "
			+ "(#{id}, #{name}, #{code}, #{field}, #{remark}, #{createBy}, now(), #{status}, #{deleteFlag})")
	int addPrintBus(PrintBusinessBean bean);

	/**
	 * 修改打印业务
	 * @param bean
	 * @return
	 */
	@Update("<script> \n"
			+ "UPDATE c_print_business SET update_by = #{updateBy}, update_time = now()"
			+ "<if test=\"name !=null and name !=''\">, name = #{name}</if>"
			+ "<if test=\"code !=null and code !=''\">, code = #{code}</if>"
			+ "<if test=\"field !=null and field !=''\">, field = #{field}</if>"
			+ "<if test=\"remark !=null and remark !=''\">, remark = #{remark}</if>"
			+ "<if test=\"status !=null\">, status = #{status}</if>"
			+ "<if test=\"deleteFlag !=null\">, delete_flag = #{deleteFlag}</if> "
			+ "WHERE id = #{id}"
			+ "</script>"
	)
	int updatePrintBus(PrintBusinessBean bean);

	/**
	 * 删除打印业务关联的打印模板
	 * @param id
	 * @return
	 */
	@Update("UPDATE c_print_template SET delete_flag = 1 WHERE id = #{id}")
	Integer deletePrintTemByBusId(@Param("id") String id);

	/**
	 * 修改打印模板
	 * @param bean
	 * @return
	 */
	@Update("<script> \n"
			+ "UPDATE c_print_template SET update_by = #{updateBy}, update_time = now()"
			+ "<if test=\"name !=null and name !=''\">, name = #{name}</if>"
			+ "<if test=\"content !=null and content !=''\">, content = #{content}</if>"
			+ "<if test=\"businessId !=null and businessId !=''\">, business_id = #{businessId}</if>"
			+ "<if test=\"businessCode !=null and businessCode !=''\">, business_code = #{businessCode}</if>"
			+ "<if test=\"businessGroup !=null and businessGroup !=''\">, business_group = #{businessGroup}</if>"
			+ "<if test=\"defaultFlag !=null\">, default_flag = #{defaultFlag}</if>"
			+ "<if test=\"remark !=null and remark !=''\">, remark = #{remark}</if>"
			+ "<if test=\"status !=null\">, status = #{status}</if>"
			+ "<if test=\"deleteFlag !=null\">, delete_flag = #{deleteFlag}</if> "
			+ "WHERE id = #{id}"
			+ "</script>"
	)
	int updatePrintTem(PrintTemplateBean bean);


	/**
	 * 修改默认打印模板
	 * @param bean
	 * @return
	 */
	@Update("UPDATE c_print_template SET default_flag = 0 WHERE business_id = #{businessId} AND id != #{id} ")
	int setDefaultPrintTem(@Param("businessId") String businessId,
                           @Param("id") String id);
	/**
	 * 取消已经默认的打印模板
	 * @return
	 */
	@Update("UPDATE c_print_template SET default_flag = 0 WHERE default_flag = 1")
	int setupDefaultTem();


	/**
	 * 添加打印模板
	 * @param bean
	 * @return
	 */
	@Insert("INSERT INTO c_print_template (id,name,content,business_id,default_flag,remark,create_by,create_time,delete_flag,status) "
			+ " values(#{id},#{source},#{name},#{content},#{businessId},#{defaultFlag},#{remark},#{createBy},now(),#{deleteFlag},#{status})")
	int addPrintTem(PrintTemplateBean bean);


	/**
	 * 获取某个模板
	 * @author dulingjiang
	 * @date 2020年3月26日
	 */
	@Select("SELECT id, name, business_id,default_flag,remark,create_by,status,create_time FROM c_print_template WHERE id = #{id} AND delete_flag = 0 LIMIT 1")
	PrintTemplateBean readPrintTemp(@Param("id") String id);


	//根据业务code 找  业务id
	@Select(" select * from c_print_business where code = #{businessCode}; ")
	PrintBusinessBean queryFirstByBusinessCode(@Param("businessCode")String businessCode);


}
